package cool.mtc.minghe.web.controller.sys.dict;

import cool.mtc.minghe.data.model.base.Dict;
import cool.mtc.minghe.data.model.base.IdForm;
import cool.mtc.minghe.data.model.sys.dict.type.dto.DictTypeDTO;
import cool.mtc.minghe.data.model.sys.dict.type.dto.DictTypeListDTO;
import cool.mtc.minghe.data.model.sys.dict.type.form.DictTypeForm;
import cool.mtc.minghe.data.model.sys.dict.type.query.DictTypeQuery;
import cool.mtc.minghe.service.service.sys.dict.DictTypeService;
import cool.mtc.core.page.PageData;
import cool.mtc.minghe.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 明河
 */
@Tag(name = "字典类型管理")
@RestController
@RequestMapping("api/sys/dict_type")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DictTypeController extends BaseController {
    private final DictTypeService dictTypeService;

    @PreAuthorize("@ps.hasPermission('sys:dict_type:insert')")
    @Tag(name = "字典类型管理")
    @Operation(summary = "新增", description = "权限：sys:dict_type:insert")
    @PostMapping("insert")
    public long insert(@RequestBody @Validated DictTypeForm form) {
        return dictTypeService.insert(form);
    }

    @PreAuthorize("@ps.hasPermission('sys:dict_type:update')")
    @Tag(name = "字典类型管理")
    @Operation(summary = "更新", description = "权限：sys:dict_type:update")
    @PostMapping("update")
    public void update(@RequestBody @Validated DictTypeForm form) {
        dictTypeService.update(form);
    }

    @PreAuthorize("@ps.hasPermission('sys:dict_type:delete')")
    @Tag(name = "字典类型管理")
    @Operation(summary = "删除", description = "权限：sys:dict_type:delete")
    @PostMapping("delete")
    public void delete(@RequestBody IdForm form) {
        dictTypeService.delete(form.getId());
    }

    @PreAuthorize("@ps.hasPermission('sys:dict_type:read')")
    @Tag(name = "字典类型管理")
    @Operation(summary = "分页列表", description = "权限：sys:dict_type:read")
    @GetMapping("list/page")
    public PageData<DictTypeListDTO> pageList(DictTypeQuery query) {
        return dictTypeService.pageList(query);
    }

    @PreAuthorize("@ps.hasPermission('sys:dict_type:read')")
    @Tag(name = "字典类型管理")
    @Operation(summary = "详情", description = "权限：sys:dict_type:read")
    @GetMapping("info")
    public DictTypeDTO info(Long id) {
        return dictTypeService.info(id);
    }

    @PreAuthorize("@ps.hasPermission('sys:dict_type:dict')")
    @Tag(name = "字典类型管理")
    @Operation(summary = "字典列表", description = "权限：sys:dict_type:dict")
    @GetMapping("list/dict")
    public List<Dict> dictList() {
        return dictTypeService.dictList();
    }
}
